package com.cb2.algorithm.leetcode;

/**
 * <a href='https://leetcode.cn/problems/subtract-the-product-and-sum-of-digits-of-an-integer'>整数的各位积和之差(Subtract the Product and Sum of Digits of an Integer)</a>
 * <p>给你一个整数 n，请你帮忙计算并返回该整数「各位数字之积」与「各位数字之和」的差。</p>
 *
 * <p>
 * <b>示例：</b>
 * <pre>
 *  示例 1：
 *      输入：n = 234
 *      输出：15
 *      解释：
 *          各位数之积 = 2 * 3 * 4 = 24
 *          各位数之和 = 2 + 3 + 4 = 9
 *          结果 = 24 - 9 = 15
 *
 *  示例 2：
 *      输入：n = 4421
 *      输出：21
 *      解释：
 *          各位数之积 = 4 * 4 * 2 * 1 = 32
 *          各位数之和 = 4 + 4 + 2 + 1 = 11
 *          结果 = 32 - 11 = 21
 * </pre>
 * </p>
 *
 * <p>
 * <b>提示：</b>
 * <ul>
 *     <li>1 <= n <= 10^5</li>
 * </ul>
 * </p>
 *
 * @author c2b
 * @since 2025/4/28 11:37
 */
public class LC1281SubtractProductAndSumOfDigitsOfAnInteger_S {
    static class Solution {
        public int subtractProductAndSum(int n) {
            int m = 1;
            int s = 0;
            while (n != 0) {
                int x = n % 10;
                m *= x;
                s += x;
                n /= 10;
            }
            return m - s;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.subtractProductAndSum(234));
        System.out.println(solution.subtractProductAndSum(4421));
    }
}
